<template>
  <div>
    <el-card class="asd">
      <el-form :inline="true" :model="queryInfo" class="demo-form-inline">
        <el-form-item label="目录名称">
          <el-input v-model="queryInfo.directoryName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryInfo.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.state"
              :label="item.label"
              :value="item.state"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clears()">清除</el-button>
          <el-button type="primary" @click="loadList(queryInfo)"
            >搜索</el-button
          >
        </el-form-item>
        <el-button
          @click="addpower()"
          class="addlist"
          type="success"
          icon="el-icon-edit"
          >新增权限组件</el-button
        >
      </el-form>
      <el-alert type="info"
        ><i class="el-icon-info"></i> 数据一共{{ total }}条</el-alert
      >
      <el-table :data="contantlist" style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="subjectName" label="所属学科"></el-table-column>
        <el-table-column
          prop="directoryName"
          label="目录名称"
        ></el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">{{
            scope.row.addDate | LocalizedFormats
          }}</template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state === 1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row.id, scope.row.state)"
              type="text"
              >{{ scope.row.state === 1 ? '启用' : '禁用' }}</el-button
            >
            <el-button
              type="text"
              :disabled="scope.row.state ? false : true"
              @click="
                altercontant(scope.row.subjectID, scope.row.directoryName, scope.row.id)
              "
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="scope.row.state === 1 ? false : true"
              @click="delpowerLists(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryInfo.pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="新增目录" @close="dialogFormVisibles" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="addForms">
        <el-form-item label="所属学科" label-width="120px">
          <el-select v-model="form.region" placeholder="请选择">
            <el-option
              v-for="item in studylabel"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" label-width="120px">
          <el-input
            placeholder="请输入目录名称"
            v-model="form.contantname"
            width="120px"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addcontants(form.contantname, form.region)"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog @close="dialogFormVisibles" title="新增目录" :visible.sync="alterdialogFormVisible">
      <el-form :model="form">
        <el-form-item label="所属学科" label-width="120px">
          <el-select v-model="form.region" placeholder="请选择">
            <el-option
              v-for="item in studylabel"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" label-width="120px">
          <el-input
            placeholder="请输入目录名称"
            v-model="form.contantname"
            width="120px"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="alterdialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            clickaltercontants(form.id ,form.region, form.contantname)
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getcontantList, putcontantList, studyList, addstudyList, alterstudyList, delcontantList } from '../../api/contant'

export default {
  data () {
    return {
      options: [{
        state: 1,
        label: '启用'
      }, {
        state: 0,
        label: '禁用'
      }
      ],
      queryInfo: {
        page: 1,
        pagesize: 10,
        subjectID: null,
        directoryName: null,
        state: null
      },
      contantlist: [],
      total: 0,
      dialogFormVisible: false,
      alterdialogFormVisible: false,
      form: {
        contantname: '',
        region: null,
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      studylabel: []
    }
  },
  created () {
    this.loadList(this.queryInfo)
  },
  methods: {
    async loadList (queryInfo) {
      try {
        const data = await getcontantList(queryInfo)
        // console.log(data)
        if (data.status === 200) {
          this.contantlist = data.data.items
          this.total = data.data.counts
          console.log(this.contantlist)
        } else {
          this.$message.error('获取失败')
        }
      } catch (err) {
        this.$message.error('错了哦.失败,请重试')
      }
    },
    clears () {
      this.queryInfo.directoryName = ''
      this.queryInfo.state = null
    },
    async handleClick (id, state) {
      await putcontantList(id, state)
      this.loadList(this.queryInfo)
      this.$message.success('成功')
      // console.log(data)
    },
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.loadList(this.queryInfo)
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      console.log(newPage)
      this.queryInfo.page = newPage
      this.loadList(this.queryInfo)
    },
    stateChange (value) {
      console.log(value)
    },
    async addpower () {
      this.dialogFormVisible = true
      const data = await studyList()
      // console.log(data)
      this.studylabel = data.data
    },
    dialogFormVisibles () {
      this.form.contantname = ''
      this.form.region = null
      // this.$refs.addForms.resetFields()
    },
    async addcontants (cname, region) {
      await addstudyList({
        subjectID: region,
        directoryName: cname
      })
      this.dialogFormVisible = false
      this.loadList(this.queryInfo)
    },
    async altercontant (a, b, id) {
      this.alterdialogFormVisible = true
      const data = await studyList()
      // console.log(data)
      this.studylabel = data.data
      // this.addpower()
      this.form.contantname = b
      this.form.region = a
      this.form.id = id
    },
    async clickaltercontants (id, c, d) {
      var data = {
        id: id,
        subjectID: c,
        directoryName: d
      }
      await alterstudyList(id, data)
      this.loadList(this.queryInfo)
      this.alterdialogFormVisible = false
    },
    async delpowerLists (id) {
      const deluserlilst = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // console.log(deluserlilst)
      if (deluserlilst !== 'confirm') {
        return this.$message.info('删除取消!')
      }
      // console.log(id)
      await delcontantList(id)
      this.loadList(this.queryInfo)
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
.el-pagination {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
.asd {
  position: relative;
}
.addlist {
  position: absolute;
  right: 21px;
}
/deep/ .el-dialog__header {
  background: #409eff;
}
/deep/ .el-dialog {
  overflow: hidden;
  border-radius: 9px 9px;
}
// /deep/ .el-input > .el-input__inner {
//   width: 200px;
// }
</style>
